package wqh.shui_ya.modbus;

import cn.tpson.hk.core.HKDTServer;
import cn.tpson.hk.dto.RealTimeValueMessage;
import cn.tpson.hk.dto.UploadAlarmMessage;
import cn.tpson.modbus.core.IDataCallBack;
import org.springframework.beans.factory.annotation.Autowired;
import wqh.domain.WaterChannelAlarm;
import wqh.global.GlobalID;
import wqh.global.GlobalWaterAlarm;
import wqh.global.GlobalWaterChannel;
import wqh.global.GlobalWaterDevice;
import wqh.service.WaterChannelAlarmService;
import wqh.service.WaterDeviceService;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by devts on 2016/12/15.
 */
public class WaterDataCallBack implements IDataCallBack {

    @Autowired
    private WaterDeviceService waterDeviceService;

    @Autowired
    private WaterChannelAlarmService waterChannelAlarmService;


    public void poll(String s, float v, float v1, float v2, float v3) {

    }

    public void poll(String s, int i, float v, float v1, float v2, float v3) {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("channel_1",v);
        map.put("channel_2",v1);
        map.put("channel_3",v2);
        map.put("channel_4",v3);
        map.put("time",new Long(System.currentTimeMillis() / 1000).intValue());
        GlobalWaterDevice.addDevice(s + ":" + i,map);
        System.out.println(map);

        long device_id = waterDeviceService.findIdByIpAndPort(s,i);

        System.out.println("发送水压数据");
        RealTimeValueMessage rtMsg1 = new RealTimeValueMessage();
        RealTimeValueMessage rtMsg2 = new RealTimeValueMessage();
        RealTimeValueMessage rtMsg3 = new RealTimeValueMessage();
        RealTimeValueMessage rtMsg4 = new RealTimeValueMessage();
        rtMsg1.setHostId(String.valueOf(device_id));
        rtMsg1.setDeviceAddress("1");
        rtMsg1.setValue(String.valueOf(v));
        HKDTServer.getInstance().addMessage(rtMsg1);

        rtMsg2.setHostId(String.valueOf(device_id));
        rtMsg2.setDeviceAddress("2");
        rtMsg2.setValue(String.valueOf(v1));
        HKDTServer.getInstance().addMessage(rtMsg2);

        rtMsg3.setHostId(String.valueOf(device_id));
        rtMsg3.setDeviceAddress("3");
        rtMsg3.setValue(String.valueOf(v2));
        HKDTServer.getInstance().addMessage(rtMsg3);

        rtMsg4.setHostId(String.valueOf(device_id));
        rtMsg4.setDeviceAddress("4");
        rtMsg4.setValue(String.valueOf(v3));
        HKDTServer.getInstance().addMessage(rtMsg4);

        Object h1 = GlobalWaterChannel.getData(s + ":" + i +  "H" + 1);
        Object l1 = GlobalWaterChannel.getData(s + ":" + i +  "L" + 1);
        Object h2 = GlobalWaterChannel.getData(s + ":" + i +  "H" + 2);
        Object l2 = GlobalWaterChannel.getData(s + ":" + i +  "L" + 2);
        Object h3 = GlobalWaterChannel.getData(s + ":" + i +  "H" + 3);
        Object l3 = GlobalWaterChannel.getData(s + ":" + i +  "L" + 3);
        Object h4 = GlobalWaterChannel.getData(s + ":" + i +  "H" + 4);
        Object l4 = GlobalWaterChannel.getData(s + ":" + i +  "L" + 4);

        if(h1 != null){
            if(v > (Float) h1){
                UploadAlarmMessage alarm = new UploadAlarmMessage();
                alarm.setHostId(String.valueOf(device_id));
                alarm.setDeviceAddress("1");
                alarm.setAlarmType("10004");
                alarm.setAlarmValue(String.valueOf(v));
                String time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
                alarm.setAlarmTime(time);
                HKDTServer.getInstance().addMessage(alarm);
                GlobalWaterAlarm.addStatus(device_id + "@" + 1,true);
                waterChannelAlarmService.save(new WaterChannelAlarm(new Long(System.currentTimeMillis() / 1000).intValue(),
                                                                    "高报",
                                                                     device_id,
                                                                     1,
                                                                     v));
            }else{
                GlobalWaterAlarm.addStatus(device_id + "#" + 1,false);
            }
        }
        if(l1 != null){
            if(v < (Float) l1){
                UploadAlarmMessage alarm = new UploadAlarmMessage();
                alarm.setHostId(String.valueOf(device_id));
                alarm.setDeviceAddress("1");
                alarm.setAlarmType("10004");
                alarm.setAlarmValue(String.valueOf(v));
                String time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
                alarm.setAlarmTime(time);
                HKDTServer.getInstance().addMessage(alarm);
                GlobalWaterAlarm.addStatus(device_id + "@" + 1,true);
                waterChannelAlarmService.save(new WaterChannelAlarm(new Long(System.currentTimeMillis() / 1000).intValue(),
                        "低报",
                        device_id,
                        1,
                        v));
            }else{
                GlobalWaterAlarm.addStatus(device_id + "#" + 1,false);
            }
        }
        if(h2 != null){
            if(v1 > (Float) h2){
                UploadAlarmMessage alarm = new UploadAlarmMessage();
                alarm.setHostId(String.valueOf(device_id));
                alarm.setDeviceAddress("2");
                alarm.setAlarmType("10004");
                alarm.setAlarmValue(String.valueOf(v));
                String time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
                alarm.setAlarmTime(time);
                HKDTServer.getInstance().addMessage(alarm);
                GlobalWaterAlarm.addStatus(device_id + "@" + 2,true);
                waterChannelAlarmService.save(new WaterChannelAlarm(new Long(System.currentTimeMillis() / 1000).intValue(),
                        "高报",
                        device_id,
                        2,
                        v));
            }else{
                GlobalWaterAlarm.addStatus(device_id + "#" + 2,false);
            }
        }
        if(l2 != null){
            if(v1 < (Float) l2){
                UploadAlarmMessage alarm = new UploadAlarmMessage();
                alarm.setHostId(String.valueOf(device_id));
                alarm.setDeviceAddress("2");
                alarm.setAlarmType("10004");
                alarm.setAlarmValue(String.valueOf(v));
                String time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
                alarm.setAlarmTime(time);
                HKDTServer.getInstance().addMessage(alarm);
                GlobalWaterAlarm.addStatus(device_id + "@" + 2,true);
                waterChannelAlarmService.save(new WaterChannelAlarm(new Long(System.currentTimeMillis() / 1000).intValue(),
                        "低报",
                        device_id,
                        2,
                        v));
            }else{
                GlobalWaterAlarm.addStatus(device_id + "#" + 2,false);
            }
        }
        if(h3!= null){
            if(v1 > (Float) h3){
                UploadAlarmMessage alarm = new UploadAlarmMessage();
                alarm.setHostId(String.valueOf(device_id));
                alarm.setDeviceAddress("3");
                alarm.setAlarmType("10004");
                alarm.setAlarmValue(String.valueOf(v));
                String time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
                alarm.setAlarmTime(time);
                HKDTServer.getInstance().addMessage(alarm);
                GlobalWaterAlarm.addStatus(device_id + "@" + 3,true);
                waterChannelAlarmService.save(new WaterChannelAlarm(new Long(System.currentTimeMillis() / 1000).intValue(),
                        "高报",
                        device_id,
                        3,
                        v));
            }else{
                GlobalWaterAlarm.addStatus(device_id + "#" + 3,false);
            }
        }
        if(l3 != null){
            if(v2 < (Float) l3){
                UploadAlarmMessage alarm = new UploadAlarmMessage();
                alarm.setHostId(String.valueOf(device_id));
                alarm.setDeviceAddress("3");
                alarm.setAlarmType("10004");
                alarm.setAlarmValue(String.valueOf(v));
                String time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
                alarm.setAlarmTime(time);
                HKDTServer.getInstance().addMessage(alarm);
                GlobalWaterAlarm.addStatus(device_id + "@" + 3,true);
                waterChannelAlarmService.save(new WaterChannelAlarm(new Long(System.currentTimeMillis() / 1000).intValue(),
                        "低报",
                        device_id,
                        3,
                        v));
            }else{
                GlobalWaterAlarm.addStatus(device_id + "#" + 3,false);
            }
        }
        if(h4 != null){
            if(v1 > (Float) h4){
                UploadAlarmMessage alarm = new UploadAlarmMessage();
                alarm.setHostId(String.valueOf(device_id));
                alarm.setDeviceAddress("4");
                alarm.setAlarmType("10004");
                alarm.setAlarmValue(String.valueOf(v));
                String time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
                alarm.setAlarmTime(time);
                HKDTServer.getInstance().addMessage(alarm);
                GlobalWaterAlarm.addStatus(device_id + "@" + 4,true);
                waterChannelAlarmService.save(new WaterChannelAlarm(new Long(System.currentTimeMillis() / 1000).intValue(),
                        "高报",
                        device_id,
                        4,
                        v));
            }else{
                GlobalWaterAlarm.addStatus(device_id + "#" + 4,false);
            }
        }
        if(l4 != null){
            if(v1 < (Float) l4){
                UploadAlarmMessage alarm = new UploadAlarmMessage();
                alarm.setHostId(String.valueOf(device_id));
                alarm.setDeviceAddress("4");
                alarm.setAlarmType("10004");
                alarm.setAlarmValue(String.valueOf(v));
                String time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
                alarm.setAlarmTime(time);
                HKDTServer.getInstance().addMessage(alarm);
                GlobalWaterAlarm.addStatus(device_id + "@" + 4,true);
                waterChannelAlarmService.save(new WaterChannelAlarm(new Long(System.currentTimeMillis() / 1000).intValue(),
                        "低报",
                        device_id,
                        4,
                        v));
            }else{
                GlobalWaterAlarm.addStatus(device_id + "#" + 4,false);
            }
        }
    }

    public void onLine(String s, boolean b) {

    }

    public void onLine(String s, int i, boolean b) {
        if(b){
            GlobalWaterDevice.addDevice(s + ":" + i,null);
            System.out.println("上线IP:" + s + ":" + i);
            long device_id = waterDeviceService.findIdByIpAndPort(s,i);
            GlobalID.addID(String.valueOf(device_id));
        }else{
            GlobalWaterDevice.removeDevice(s + ":" + i);
            System.out.println("下线IP:" + s + ":" + i);
            long device_id = waterDeviceService.findIdByIpAndPort(s,i);
            GlobalID.removeID(String.valueOf(device_id));
        }
    }

    public void fail(String s, String s1) {

    }

    public void fail(String s, int i, String s1) {
        System.out.println(s + " : " + i + " 连接失败： " + s1);
    }



}
